#include "buffer.h"
#include <iostream>
#include <string.h>


Buffer::Buffer()
{
    m_size = 0;
}

void Buffer::Read(void* buf, int len)
{
    if(m_size < len)
    {
        std::cout << "数据长度不够读取失败" << std::endl;
    }
    memcpy(buf, m_buffer, len);
    memmove(m_buffer, m_buffer + len, m_size - len);
    m_size -= len;
}

int Buffer::ReadInt()
{
    int num;
    Read(&num, sizeof(num));
    return num;
}

void Buffer::Write(const void* data, int len)
{
    if(m_size + len > 1024)
    {
        std::cout << "剩余空间不足无法写入" << std::endl;
    }
    memcpy(m_buffer + m_size, data, len);
    this->m_size += len;  
}

void Buffer::WriteInt(int value)
{
    Write(&value, sizeof(value));
}

const char* Buffer::GetBuffer()
{
    return m_buffer;
}

int Buffer::GetSize()
{
    return m_size;
}